'use strict';

const db = uniCloud.database();

exports.main = async (event, context) => {
  // 判断同一个人、序号相同的图片是否上传
  const objCount = await db.collection('images').where({
    openid: event.openid,
    image_index: event.imageIndex
  }).count();
  
  // console.log(objCount, count === 0);
  if (objCount.total === 0) { // 新增
    await db.collection('images').add({
      openid: event.openid,
      image_index: event.imageIndex,
      img_url: event.imgUrl,
      create_time: (new Date()).valueOf()
    });
  } else { // 修改
    await db.collection('images').where({
      openid: event.openid,
      image_index: event.imageIndex,
    }).update({
      img_url: event.imgUrl,
      update_time: (new Date()).valueOf()
    });
  }

  //返回数据给客户端
  return event;
};
